/// @author  Alberto Franco
/// @file    barber_shop.go
/// @date    02-05-2011
/// @brief   This in the implementation of the barber shop problem as found in
///          "The Little Book of Semaphores" by Allen B. Downey 
///          Text of the problem is below.
package barbershop

/*** BARBER SHOP PROBLEM ******************************************************
 *                                                                            *
 * A barbershop consists of a waiting room with n chairs, and the             *
 * barber room containing the barber chair. If there are no customers         * 
 * to be served, the barber goes to sleep. If a customer enters the           *
 * barbershop and all chairs are occupied, then the customer leaves           *
 * the shop. If the barber is busy, but chairs are available, then the        *
 * customer sits in one of the free chairs. If the barber is asleep, the      *
 * customer wakes up the barber. Write a program to coordinate the            *
 * barber and the customers.                                                  *
 ******************************************************************************/

import (
    "fmt"
    "time"
    "rand"
)

/// Main function.
/// Generate a barber shop with a given number of customers and a given number
/// of chairs, if no data is passed default value are used
func Barber_Main() {
    var numCust int = 10
    var barber *Barber       = CreateBarber()
    var customers []Customer = CreateCustomers(barber, numCust + 1, 2)
    
    fmt.Println("Created barber shop...")
    
    go barber.Routine()
    
    for i := 0; i < numCust; i++ {
        go customers[i].EnterShop()
        time.Sleep(rand.Int63n(700))
    }
    /// Last customer on the main routine or exit before everything finish
    customers[numCust].EnterShop() 
}

